package com.company;

import java.util.ArrayList;
import java.util.Comparator;

public class Main {

    // 1. 将数据保存到程序中【List】
    // 遍历输出所有数据
    // 计算语文的最高分
    // 计算数学的平均分
    // 统计男生和女生的人数
    // 根据英语成绩进行排序输出【从高到低】
    // ……………………

    static ArrayList<StudentScore> studentScores = new ArrayList();


    public static void main(String[] args) {
        // write your code here
        // 使用构造方法添加数据
        studentScores.add(new StudentScore("003", "王五", "男", 89, 92, 85));
        studentScores.add(new StudentScore("004", "赵六", "女", 99, 97, 92));
        studentScores.add(new StudentScore("005", "李利", "女", 78, 87, 88));
        studentScores.add(new StudentScore("006", "张三", "男", 90, 88, 78));

        // 调用功能方法
        showAll();
        System.out.println("语文最高分: " + getMaxLanguageScore());
        System.out.println("数学平均分: " + getAverageMathScore());
        System.out.println("男生人数: " + countStudentsBySex("男"));
        System.out.println("女生人数: " + countStudentsBySex("女"));
        sortByEnglishScore();
        System.out.println("按英语成绩排序后:");
        showAll();
        sortByLanguageScoreAscending();
        System.out.println("按语文成绩升序排序后:");
        showAll();
    }

    public static void showAll() {
        for (int i = 0; i < studentScores.size(); i++) {
            System.out.println(studentScores.get(i));
        }
    }

    public static int getMaxLanguageScore() {
        int max = 0;
        for (StudentScore score : studentScores) {
            if (score.getLanguage() > max) {
                max = score.getLanguage();
            }
        }
        return max;
    }

    public static double getAverageMathScore() {
        int sum = 0;
        for (StudentScore score : studentScores) {
            sum += score.getMath();
        }
        return (double) sum / studentScores.size();
    }

    public static int countStudentsBySex(String sex) {
        int count = 0;
        for (StudentScore score : studentScores) {
            if (score.getSex().equals(sex)) {
                count++;
            }
        }
        return count;
    }


    public static void sortByEnglishScore() {
        studentScores.sort(new Comparator<StudentScore>() {
            @Override
            public int compare(StudentScore s1, StudentScore s2) {
                return Integer.compare(s2.getEnglish(), s1.getEnglish()); //用了ai
            }
        });
    }

    public static void sortByLanguageScoreAscending() {
        studentScores.sort(new Comparator<StudentScore>() {
            @Override
            public int compare(StudentScore s1, StudentScore s2) {
                return Integer.compare(s1.getLanguage(), s2.getLanguage());
            }
        });
    }
}













